最後在main裡重置BookCreated
事件
let old_events = old_serialized_events();
let _: Vec<SerializedEvent> = db
.create("events")
.content(old_events[0].clone())
.await?;
// let _ = book_cqrs.execute("test-book-1", BookCommand::CreateBook {
// id: "test-book-1".to_string(),
// title: "Rust 語言開發實戰".to_string(),
// isbn10: "1234567890".to_string(),
// description: "使用rust,併同cqrs框架,實現event sourcing".to_string(),
// author: "John Smith".to_string(),
// }).await;
let _: Vec<BookDto> = db
.create("books")
.content(BookDto {
book_id: "test-book-1".to_string(),
title: "Rust 語言開發實戰".to_string(),
isbn10: "1234567890".to_string(),
description: "使用rust,併同cqrs框架,實現event sourcing".to_string(),
copies: 0,
available_copies: 0,
// author: None,
}).await?;
要補上DTO,之前book Dto會在執行book_cqrs.execute
的時候產生,但我們模擬舊版事件,所以會是空的,沒補的話未來的事件會執行失敗。
補完後結果:
======== [Source of Truth] Events ========
[
SerializedEvent {
aggregate_id: "test-book-1",
sequence: 1,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.1.0",
payload: Object {
"BookCreated": Object {
"description": String("使用rust,併同cqrs框架,實現event sourcing"),
"id": String("test-book-1"),
"isbn10": String("1234567890"),
"title": String("Rust 語言開發實戰"),
},
},
metadata: Object {},
},
SerializedEvent {
aggregate_id: "test-book-2",
sequence: 1,
aggregate_type: "Book",
event_type: "BookEvent",
event_version: "0.2.0",
payload: Object {
"BookCreated": Object {
"author": String("John Smith"),
"description": String("看看作者欄位有沒有寫入"),
"id": String("test-book-2"),
"isbn10": String("0000000000"),
"title": String("新書新事件"),
},
},
metadata: Object {},
},
]
可以看到event soucing裡同時併專不同版本的事件,而後面的操作也如常運作。
======== [Query] Book ========
[
BookDto {
book_id: "test-book-1",
title: "Rust 語言開發實戰",
isbn10: "1234567890",
description: "使用rust,併同cqrs框架,實現event sourcing",
copies: 2,
available_copies: 1,
},
BookDto {
book_id: "test-book-2",
title: "新書新事件",
isbn10: "0000000000",
description: "看看作者欄位有沒有寫入",
copies: 0,
available_copies: 0,
},
]